/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JP_RegistrarResultado.java
 *
 * Created on 16/07/2009, 03:57:11 PM
 */

package gui;
import delegate.GestionDelegate;
import controlacciones.ejb3.entity.TbAccion;
import controlacciones.ejb3.entity.TbResultado;
import controlacciones.ejb3.entity.TbTipoEjecucion;
import controlacciones.ejb3.entity.TbTipoObservacion;
import helper.JP_Utiles_PanelBotonesMantenimiento;
import helper.JP_Utiles_PanelModelo;
import helper.Utiles_Fecha_Cliente;
import helper.Utiles_Importar;
import helper.Utiles_MensajesDialogo;
import helper.Utiles_Varios;
import java.awt.BorderLayout;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.jdesktop.application.Task;
/**
 *
 * 
 */
public class JP_RegistrarResultado extends JP_Utiles_PanelModelo {

    /** Creates new form JP_RegistrarResultado */
    public JP_RegistrarResultado() {
        inicializaComponentes();
    }

    private void inicializaComponentes(){
        initComponents();
        //Panel de Operaciones
        ArrayList alBotones = new ArrayList( 1 );
        alBotones.add( new Integer( JP_Utiles_PanelBotonesMantenimiento.BOTON_GUARDAR ) );
        alBotones.add( new Integer( JP_Utiles_PanelBotonesMantenimiento.BOTON_CERRAR ) );
        jp_ope = new JP_Utiles_PanelBotonesMantenimiento( this, alBotones );
        jp_Operaciones.setLayout( new BorderLayout() );
        jp_Operaciones.add( jp_ope, BorderLayout.CENTER );
        this.listaTbResultado = new ArrayList(1);
    }

    private void cargaTabla(){
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaTbResultado, this.tbResultadoAcciones);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idAccion}"));
        columnBinding.setColumnName("Id. Acción.");
        columnBinding.setColumnClass(Integer.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding2 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codOpe1}"));
        columnBinding2.setColumnName("Cod. Operador1");
        columnBinding2.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding3 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codOpe1}"));
        columnBinding3.setColumnName("Cod. Operador2");
        columnBinding3.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding4 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codSupervisor}"));
        columnBinding4.setColumnName("Cod. Supervisor");

        columnBinding4.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding5 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${lecturaMedidor}"));
        columnBinding5.setColumnName("Lectura");
        columnBinding5.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding6 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fechaEjecucion}"));
        columnBinding6.setColumnName("Fecha Ejecucion");
        columnBinding6.setColumnClass(Date.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding7 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fechaRecepcion}"));
        columnBinding7.setColumnName("Fecha Recepcion");
        columnBinding7.setColumnClass(Date.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding8 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codTipoEjecucion}"));
        columnBinding8.setColumnName("Tipo de Ejecucion");
        columnBinding8.setColumnClass(TbTipoEjecucion.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding9 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codTipoObservacion}"));
        columnBinding9.setColumnName("Tipo de Observacion");
        columnBinding9.setColumnClass(TbTipoObservacion.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding10 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${observacionResultado}"));
        columnBinding10.setColumnName("Observacion");
        columnBinding10.setColumnClass(String.class);

        bindingGroup.addBinding(jTableBinding);
        bindingGroup.bind();
    }

    public void refrescarTabla(){
        try{
            this.bindingGroup.removeBinding(jTableBinding);
        }catch( Exception e ){

        }
        this.cargaTabla();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jp_Derechohabientes = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbResultadoAcciones = new javax.swing.JTable();
        btnImportar = new javax.swing.JButton();
        jp_Operaciones = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(controlaccionescomercial.ControlAccionesComercialApp.class).getContext().getResourceMap(JP_RegistrarResultado.class);
        jp_Derechohabientes.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jp_Derechohabientes.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jp_Derechohabientes.border.titleFont"))); // NOI18N
        jp_Derechohabientes.setName("jp_Derechohabientes"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tbResultadoAcciones.setAutoCreateRowSorter(true);
        tbResultadoAcciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbResultadoAcciones.setName("tbResultadoAcciones"); // NOI18N
        jScrollPane1.setViewportView(tbResultadoAcciones);

        btnImportar.setFont(resourceMap.getFont("btnImportar.font")); // NOI18N
        btnImportar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/images/search-folder-blue.gif"))); // NOI18N
        btnImportar.setText(resourceMap.getString("btnImportar.text")); // NOI18N
        btnImportar.setName("btnImportar"); // NOI18N
        btnImportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImportarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jp_DerechohabientesLayout = new javax.swing.GroupLayout(jp_Derechohabientes);
        jp_Derechohabientes.setLayout(jp_DerechohabientesLayout);
        jp_DerechohabientesLayout.setHorizontalGroup(
            jp_DerechohabientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jp_DerechohabientesLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnImportar))
        );
        jp_DerechohabientesLayout.setVerticalGroup(
            jp_DerechohabientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_DerechohabientesLayout.createSequentialGroup()
                .addComponent(btnImportar)
                .addContainerGap(139, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
        );

        jp_Operaciones.setName("jp_Operaciones"); // NOI18N

        javax.swing.GroupLayout jp_OperacionesLayout = new javax.swing.GroupLayout(jp_Operaciones);
        jp_Operaciones.setLayout(jp_OperacionesLayout);
        jp_OperacionesLayout.setHorizontalGroup(
            jp_OperacionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 711, Short.MAX_VALUE)
        );
        jp_OperacionesLayout.setVerticalGroup(
            jp_OperacionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 45, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jp_Derechohabientes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jp_Operaciones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jp_Derechohabientes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jp_Operaciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnImportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImportarActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setMultiSelectionEnabled( false );
        if( ruta != null ){
            jfc.setCurrentDirectory( new File( ruta ) );
        }
        int returnVal = jfc.showOpenDialog( this );
        if( returnVal == JFileChooser.APPROVE_OPTION ){
            File arch = new File( jfc.getSelectedFile().toString() );
            Utiles_Importar utiles_importar = new Utiles_Importar( arch );
            this.ruta = arch.getParent(); //Asignamos la ruta guardando temporalmente la ruta donde nos hemos quedado
            try{
                List dat = utiles_importar.getDatosHoja( 0 ); //Definimos obtener los datos de la hoja 0

                List tempObj = null;
                TbResultado resultado;
                //TbAccion accion;
                TbTipoEjecucion tipoEjecucion;
                TbTipoObservacion tipoObservacion;

                this.listaTbResultado.clear();
                
                for( Object obj : dat ){
                    tempObj = (List) obj;
                    tipoEjecucion = new TbTipoEjecucion();
                    tipoObservacion = new TbTipoObservacion();
                    resultado = new TbResultado();
                    System.out.println(tempObj);
                    resultado.setIdAccion( new Integer( tempObj.get(0).toString()) );
                    tipoEjecucion.setCodTipoEjecucion((String) tempObj.get(7));
                    tipoObservacion.setCodTipoObservacion((String) tempObj.get(8));
                    resultado.setCodOpe1((String) tempObj.get(1));
                    resultado.setCodOpe2((String) tempObj.get(2));
                    resultado.setCodSupervisor((String) tempObj.get(3));
                    resultado.setLecturaMedidor((String) tempObj.get(4));
                    Utiles_Fecha_Cliente fechaEjecucion = new Utiles_Fecha_Cliente( tempObj.get(5).toString() );
                    Utiles_Fecha_Cliente fechaRecepcion = new Utiles_Fecha_Cliente( tempObj.get(6).toString() );
                    resultado.setFechaEjecucion(fechaEjecucion.getDate());
                    resultado.setFechaRecepcion(fechaRecepcion.getDate());
                    resultado.setCodTipoEjecucion(tipoEjecucion);
                    resultado.setCodTipoObservacion(tipoObservacion);
                    resultado.setObservacionResultado((String) tempObj.get(9));
                    this.listaTbResultado.add( resultado );
                }
                Utiles_Varios.centraCeldasTabla( this.tbResultadoAcciones );
            }catch( Exception er ){
                Utiles_MensajesDialogo.mensajeAlerta( "Verifique si el archivo que desea cargar es el correcto" );
                er.printStackTrace();
            }
            this.refrescarTabla();
        }
    }//GEN-LAST:event_btnImportarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnImportar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jp_Derechohabientes;
    private javax.swing.JPanel jp_Operaciones;
    private javax.swing.JTable tbResultadoAcciones;
    // End of variables declaration//GEN-END:variables

    @Override
    public Task guardar() {
        try {
            GestionDelegate.getInstance().registrarResultadoAcciones(listaTbResultado);
        } catch (Exception ex) {
            Logger.getLogger(JP_RegistrarResultado.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
   
    @Override
    public Task cerrar() {
        this.cerrarPadre();
        return null;
    }

    private org.jdesktop.swingbinding.JTableBinding jTableBinding;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    private JP_Utiles_PanelBotonesMantenimiento jp_ope;
    private String ruta;

    private List<TbResultado> listaTbResultado;
}